<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="任务编号" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务单号" prop="danhao">
        <el-input
          v-model="queryParams.danhao"
          placeholder="请输入业务单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发起人编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入发起人编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批人工号" prop="spUserId">
        <el-input
          v-model="queryParams.spUserId"
          placeholder="请输入审批人工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批流编码" prop="spCode">
        <el-input
          v-model="queryParams.spCode"
          placeholder="请输入审批流编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批流名称" prop="spName">
        <el-input
          v-model="queryParams.spName"
          placeholder="请输入审批流名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:spTask:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:spTask:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:spTask:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:spTask:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工厂名称" align="center" prop="factoryName" />
      <el-table-column label="工厂代码" align="center" prop="factoryCode" />
      <el-table-column label="任务编号" align="center" prop="taskId" />
      <el-table-column label="业务单号" align="center" prop="danhao" />
      <el-table-column label="审批流编码" width="100" align="center" prop="spCode" />
      <el-table-column label="审批流名称" width="100" align="center" prop="spName" />
      <el-table-column label="发起人姓名" width="100" align="center" prop="userName" />
      <el-table-column label="发起人编号" width="100" align="center" prop="userId" />
      <el-table-column label="发起部门编码" width="100" align="center" prop="deptId" />
      <el-table-column label="当前审批人工号" width="100" align="center" prop="spUserId" />
      <el-table-column label="当前审批人姓名" width="100" align="center" prop="spUserName" />
      <el-table-column label="审批节点" width="100" align="center" prop="spSolt"  />
      <el-table-column label="审批状态" width="100" align="center" prop="spFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sp_flag" :value="scope.row.spFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="审批建议" width="100" show-overflow-tooltip align="center" prop="spRemark" />
      <el-table-column label="是否审批完成" width="100" align="center" prop="isSpwc" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_spwc" :value="scope.row.isSpwc"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" show-overflow-tooltip align="center" prop="remark" />
      <el-table-column width="180" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template  slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleShenPi(scope.row)"
            v-if="scope.row.isSpwc != 1"
            v-hasPermi="['system:spTask:sp']"
          >审批</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            v-hasPermi="['system:spTask:splc']"
            @click="handleShenPiLc(scope.row)"
          >审批流程</el-button>
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:spTask:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:spTask:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改审批任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="工厂名称" prop="factoryName">
              <el-input v-model="form.factoryName" placeholder="请输入工厂名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工厂代码" prop="factoryCode">
              <el-input v-model="form.factoryCode" placeholder="请输入工厂代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务编号" prop="taskId">
              <el-input v-model="form.taskId" placeholder="请输入任务编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务单号" prop="danhao">
              <el-input v-model="form.danhao" placeholder="请输入业务单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发起人姓名" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入发起人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发起人编号" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入发起人编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发起部门编码" prop="deptId">
              <el-input v-model="form.deptId" placeholder="请输入发起部门编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批人工号" prop="spUserId">
              <el-input v-model="form.spUserId" placeholder="请输入审批人工号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批人姓名" prop="spUserName">
              <el-input v-model="form.spUserName" placeholder="请输入审批人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批流编码" prop="spCode">
              <el-input v-model="form.spCode" placeholder="请输入审批流编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批流名称" prop="spName">
              <el-input v-model="form.spName" placeholder="请输入审批流名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批节点" prop="spSolt">
              <el-input v-model="form.spSolt" placeholder="请输入审批节点" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批状态" prop="spFlag">
              <el-input v-model="form.spFlag" placeholder="请输入审批状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批建议" prop="spRemark">
              <el-input v-model="form.spRemark" placeholder="请输入审批建议" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否审批完成" prop="isSpwc">
              <el-input v-model="form.isSpwc" placeholder="请输入是否审批完成" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改采购单对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="sp.title" :visible.sync="sp.open" width="1400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="审批状态" prop="spFlag">
          <el-radio v-model="form.spFlag" label="1">同意</el-radio>
          <el-radio v-model="form.spFlag" label="2">不同意</el-radio>
        </el-form-item>
        <el-form-item label="审批意见" prop="spRemak">
          <el-input style="width: 500px"  type="textarea" v-model="form.spRemark" placeholder="请输入审批意见" />
        </el-form-item>
      </el-form>
      <PurchaseInfoView ref="purchaseInfoView"></PurchaseInfoView>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSpForm">确 定</el-button>
        <el-button @click="sp.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog  :close-on-click-modal="false" :title="splc.title" :visible.sync="splc.open" width="1200px" append-to-body>
      <el-table v-loading="loading" :data="spTaskProcessList" @selection-change="handleSelectionChange">
        <el-table-column label="任务编号" align="center" prop="taskId" />
        <el-table-column label="审批人" align="center" prop="remark" />
        <el-table-column label="审批节点" align="center" prop="spSolt" />
        <el-table-column label="审批状态" align="center" prop="spFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sp_flag" :value="scope.row.spFlag"/>
        </template>
        </el-table-column>
        <el-table-column label="审批建议" align="center" prop="spRemark" />
        <el-table-column label="审批时间" align="center" prop="createTime" />
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button @click="splc.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listSpTask, getSpTask, delSpTask, addSpTask, updateSpTask,spSubTask} from "@/api/system/spTask";
// import PurchaseInfoView from '@/views/business/purchaseInfo/purchaseInfoView';
import {getToken} from "@/utils/auth";
import PurchaseInfoView from "../../business/purchaseInfo/purchaseInfoView";
import { listSpTaskProcess} from "@/api/system/spTaskProcess";

export default {
  components: {
    PurchaseInfoView
  },
  name: "SpTask",
  dicts: ['sp_flag', 'is_spwc'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审批任务表格数据
      spTaskList: [],
      spTaskProcessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      sp:{
        title: "",
        // 是否显示弹出层
        open: false,
      },
      splc:{
        title: "",
        // 是否显示弹出层
        open: false,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        danhao: null,
        userId: null,
        spUserId: null,
        spCode: null,
        spName: null,
      },
      // 查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 20,
        taskId: null,
      },
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "导入`spTask`信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/spTask/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询审批任务列表 */
    getList() {
      this.loading = true;
      listSpTask(this.queryParams).then(response => {
        this.spTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryName: null,
        factoryCode: null,
        taskId: null,
        danhao: null,
        userName: null,
        userId: null,
        deptId: null,
        spUserId: null,
        spUserName: null,
        spCode: null,
        spName: null,
        spSolt: null,
        spFlag: null,
        spRemark: null,
        isSpwc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审批任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审批任务";
      });
    },
    /** 采购单审批 */
    handleShenPi(row) {
      this.reset();
      const id = row.id
      this.sp.open = true;
      this.sp.title = "任务审批";
      this.form.id = id;
      this.form=row;
      this.form.spFlag="1";
      setTimeout(() => {
        this.$refs.purchaseInfoView.getViewList(row.danhao);
      }, 10);
    },
    handleShenPiLc(row) {
      this.reset();
      this.splc.open = true;
      this.splc.title = "审批任务流程";
      this.queryParams2.taskId=row.taskId
      listSpTaskProcess(this.queryParams2).then(response => {
        this.spTaskProcessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    submitSpForm() {
      spSubTask(this.form).then(response => {
        this.$modal.msgSuccess("审批成功");
        this.sp.open = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据？').then(function() {
        return delSpTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/spTask/export', {
        ...this.queryParams
      }, `spTask_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "审批任务导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/spTask/importTemplate', {
      }, `审批任务模板.xlsx`)
    },
    /** 文件上传中处理 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 文件上传成功处理 */
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 提交上传文件 */
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
